package org.xawl.news.servlet.role;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.xawl.news.dao.impl.role.RoleDaoImpl;
import org.xawl.news.dao.role.RoleDao;
import org.xawl.news.dao.user.UserDao;
import org.xawl.news.entity.role.Role;
import org.xawl.news.servlet.base.BaseServlet;
import org.xawl.news.util.AjaxJson;
import org.xawl.news.util.StringUtil;

import com.alibaba.fastjson.JSON;

public class RoleServlet extends BaseServlet {

	private static final long serialVersionUID = 1L;
	private RoleDao roleDao = new RoleDaoImpl();
	
	/**
	 * @Title: index
	 * @Description: 角色主页
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 * @return: String
	 */
	protected String list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<Role> roles = roleDao.findList();
		int count = roleDao.findCount();
		request.setAttribute("roles", roles);
		request.setAttribute("count", count);
		return "forward:system/role/roleList";
	}
	
	
	/**
	 * 
	 * @Title: add 
	 * @Description: 角色添加
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 * @return: String
	 */
	protected String add(HttpServletRequest request, HttpServletResponse response) {
		return "forward:system/role/roleAdd";
	}
	
	/**
	 * 
	 * @Title: roleSave
	 * @Description: 角色录入保存
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 * @return: String
	 */
	protected void save(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		AjaxJson j = new AjaxJson();
		String name = request.getParameter("name");
		String code = request.getParameter("code");
		String description = request.getParameter("description");
		Role role = new Role();
		role.setName(name);
		role.setCode(code);
		role.setDescription(description);
		role.setCreateTime(new Date());
		role.setUpdateTime(new Date());
		boolean flag = roleDao.save(role);
		if(flag) {
			j.setMsg("添加角色成功!");
			j.setSuccess(true);
		} else {
			j.setMsg("添加角色失败!");
			j.setSuccess(false);
		}
		response.getWriter().write(JSON.toJSONString(j));
	}
	
	
	/**
	 * 
	 * @Title: roleSave
	 * @Description: 角色录入保存
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 * @return: String
	 */
	protected void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		AjaxJson j = new AjaxJson();
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String code = request.getParameter("code");
		String description = request.getParameter("description");
		if(StringUtil.isNotEmpty(id)) {
			 Role role = roleDao.findById(Integer.valueOf(id).intValue());
			 role.setCode(code);
			 role.setName(name);
			 role.setDescription(description);
			 role.setUpdateTime(new Date());
			 
			 boolean flag = roleDao.update(role);
			 if(flag) {
				 j.setMsg("添加角色成功!");
				 j.setSuccess(true);
			 } else {
				j.setMsg("添加角色失败!");
				j.setSuccess(false);
			 }
		} else {
			j.setMsg("角色编号不能为空!");
			j.setSuccess(false);
		}
		response.getWriter().write(JSON.toJSONString(j));
	}
	
	/**
	 * 
	 * @Title: edit 
	 * @Description: 角色编辑
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 * @return: String
	 */
	protected String edit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id = request.getParameter("id");
		Role role = null;
		if(StringUtil.isNotEmpty(id)) {
			 role = roleDao.findById(Integer.valueOf(id).intValue());
		}
		request.setAttribute("role", role);
		return "forward:system/role/roleEdit";
	}
	
	/**
	 * 
	 * @Title: delete 
	 * @Description: 角色删除
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 * @return: String
	 */
	protected void del(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		AjaxJson j = new AjaxJson();
		String id = request.getParameter("id");
		if(StringUtil.isEmpty(id)) {
			j.setSuccess(false);
			j.setMsg("删除记录编号不能为空!");
		} else {
			boolean flag = roleDao.delete(Integer.valueOf(id).intValue());
			if(flag) {
				j.setMsg("删除角色成功!");
				j.setSuccess(true);
			} else {
				j.setMsg("删除角色失败!");
				j.setSuccess(false);
			}
		}
		response.getWriter().write(JSON.toJSONString(j));
	}
}
